﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.MODEL.CustomerManage;
using MINERP.BLL.CustomerManage;
using MINERP.MODEL.FoundationModel;
using MINERP.ComControls;
using MINERP.ComControls.Extension;

namespace MINERP.CustomerManage
{
    public partial class FrmShortOutReturn : FrmParent
    {
        public FrmShortOutReturn()
        {
            InitializeComponent();
            gridView1.ShowRowIndicator(50);
        }
        public FrmShortOutReturn(int inUserId)
        {
            InitializeComponent();
            currUserId = inUserId;
        }
        ShortOutReturnBLL _bll = new ShortOutReturnBLL();
        OrderInfoInfo CurrOrderProduct=null;
        int currUserId = 0;
        private void textEditProductId_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyValue == (int)Keys.Enter)
            {
                string productid = textEditProductId.Text;
                if (productid.Length == 10)
                {
                    productid = productid.TrimStart('2').TrimStart('0');
                }
                 CurrOrderProduct = _bll.GetOrderInfomation(Convert.ToInt32(productid));
                 if (CurrOrderProduct != null)
                {
                    
                    textEditProductName.Text = CurrOrderProduct.OrderProductName;
                    textEditShopName.Text = CurrOrderProduct.ShopInfo.ShopName;
                    textEditPageNum.Text = CurrOrderProduct.PageNum.ToString();
                    textEditNum.Text = CurrOrderProduct.ProductNum.ToString();
                    textEditSize.Text = CurrOrderProduct.Width + "x" + CurrOrderProduct.Height;
                    textEditCtmName.Text = CurrOrderProduct.CtmName;
                    comboBoxPaperType.DisplayMember = "Name";
                    comboBoxPaperType.ValueMember = "PaperTypeID";
                    comboBoxPaperType.DataSource = _bll.GetAllPaperType(CurrOrderProduct.ShopInfo.ShopId);

                    comboBoxPaperFace.DisplayMember = "Name";
                    comboBoxPaperFace.ValueMember = "PaperFaceID";
                    comboBoxPaperFace.DataSource = _bll.GetAllPaperFace();
                    gridControlPhoto.DataSource = CurrOrderProduct.Photos;
                    gridControlPhoto.RefreshDataSource();

                    comboBoxTechFlow.DisplayMember = "TechName";
                    comboBoxTechFlow.ValueMember = "TechId";
                    comboBoxTechFlow.DataSource = CurrOrderProduct.workFlows.OrderBy(p => p.WorkOrder).ToList();
                    //comboBoxPaperFace.SelectedValue=orderProduct.pa

                }
                else
                {
                    MessageBox.Show("找不到该产品的相关信息，请确认该产品存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void FrmShortOutReturn_Load(object sender, EventArgs e)
        {
           
        }

        private void buttonok_Click(object sender, EventArgs e)
        {
            int intStartTechid = 0;
            if (comboBoxTechFlow.SelectedItem != null)
            {
                WorkOrderFlowListInfo flow = (WorkOrderFlowListInfo)comboBoxTechFlow.SelectedItem;
                intStartTechid = flow.TechId;
            }
            else
            {
                MessageBox.Show("请选择流程", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (CurrOrderProduct != null)
            {
                string strVal = _bll.CreateOutReturn(CurrOrderProduct, intStartTechid, currUserId);

                if ("OK".Equals(strVal))
                {
                    MessageBox.Show("创建返单成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.CurrOrderProduct = null;
                    //textEditProductName.Text = "";
                    //textEditShopName.Text ="";
                    //textEditPageNum.Text = "";
                    //textEditNum.Text = "";
                    //textEditSize.Text = "";
                    //textEditCtmName.Text = "";
                    gridControlPhoto.DataSource =null;
                    gridControlPhoto.RefreshDataSource();
                    comboBoxTechFlow.DataSource = null;
                }
                else
                {
                    MessageBox.Show("创建返单失败，请产看错误信息：" + strVal, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("请先查找已经完成的产信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textEditNum_EditValueChanged(object sender, EventArgs e)
        {
            CurrOrderProduct.ProductNum = Convert.ToInt32(textEditNum.Value);

        }

       
    }
}
